<template>
  <div id="ent-receipts">
    <div class="card bg-transparent">
      <div class="card-header">
        <nav aria-label="breadcrumb" role="navigation">
          <ol class="breadcrumb pb-0">
            <li class="breadcrumb-item active" aria-current="page">
              收(退)款单
            </li>

            <li class="breadcrumb-item ms-auto me-2">
              <router-link to="/statement/settlements">结算单</router-link>
            </li>
          </ol>
        </nav>
      </div>
      <div class="card-body">
        <form class="row">
          <div class="col-1">
            <my-date-picker
              id="beginDate"
              v-model="beginDate"
              name="sc.beginDate"
              placeholder="开始日期"
            ></my-date-picker>
          </div>
          <div class="col-1">
            <my-date-picker
              id="endDate"
              v-model="endDate"
              name="sc.endDate"
              placeholder="截止日期"
            ></my-date-picker>
          </div>
          <div class="col-2">
            <my-select-customer
              :customerId.sync="customerId0"
              :enterpriseType="1"
            ></my-select-customer>
          </div>
          <div class="col-1">
            <input
              type="textfield"
              class="form-control"
              size="10"
              placeholder="收款单号"
              v-model.trim="receiptNo0"
            />
          </div>
          <div class="col-1">
            <input
              type="textfield"
              class="form-control"
              size="10"
              placeholder="金额"
              v-model.trim="amount0"
            />
          </div>
          <div class="col-1">
            <input
              type="textfield"
              class="form-control"
              size="20"
              placeholder="客户名称(编号)模糊查找"
              v-model.trim="likeCustomerName"
            />
          </div>
          <div class="col-1">
            <select class="form-select" v-model.number="receiptType">
              <option value="-1">全部类型</option>
              <option value="0">收款单</option>
              <option value="1">退款单</option>
            </select>
          </div>
          <div class="col-1">
            <select class="form-select" v-model.number="sc.pageSize">
              <option value="5">5</option>
              <option value="10">10</option>
              <option value="20">20</option>
              <option value="50">50</option>
              <option value="100">100</option>
              <option value="200">200</option>
              <option value="500">500</option>
              <option value="10000">10000</option>
            </select>
          </div>

          <div class="col">
            <button
              type="button"
              class="btn btn-primary ms-2"
              @click.stop="search()"
            >
              查找
            </button>
            <button
              type="button"
              class="btn btn-secondary btn-sm ms-2"
              @click.stop="reset()"
            >
              重置
            </button>
          </div>
        </form>
      </div>
    </div>
    <nav id="pagination-box-1" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>
    <table class="table table-striped table-hover table-sm">
      <thead>
        <tr>
          <th>收款单号</th>
          <th>公司名称</th>
          <th>操作员</th>
          <th class="text-end">金额</th>
          <th class="text-end">已销</th>
          <th class="text-end">剩余金额</th>
          <th>收款银行</th>
          <th>收款日期</th>
          <th>备注</th>
          <th>创建时间</th>
          <th>修改时间</th>
        </tr>
      </thead>
      <tbody>
        <tr v-if="dataList.length > 10">
          <td colspan="5" class="text-end">(剩余余额) 合计：</td>
          <td class="text-end">{{ totalBalance }}</td>
        </tr>
        <tr
          v-for="(info, index) in dataList"
          :class="{ 'bg-info text-white': info.status === 1 }"
        >
          <td>
            <span v-if="info.receiptType === 0">{{ info.receiptNo }}</span>
            <span class="text-danger" v-if="info.receiptType === 1"
              >{{ info.receiptNo }} <small>退</small></span
            >
          </td>
          <td>
            <template v-if="info.customerType === 0">散客</template>
            <template v-else-if="info.customer !== null">{{
              info.customer.vipName
            }}</template>
          </td>
          <td>{{ info.operator }}</td>
          <td class="text-end">{{ info.amount }}</td>
          <td class="text-end">{{ info.writeOffAmount }}</td>
          <td class="text-end">{{ calcBalance(info) }}</td>
          <td>{{ info.receiptBank }}</td>
          <td>{{ info.receiptDate }}</td>
          <td>
            {{ info.remark }}
          </td>
          <td>{{ info.createTime }}</td>
          <td>{{ info.lastUpdate }}</td>
        </tr>
        <tr>
          <td colspan="5" class="text-end">(剩余余额) 合计：</td>
          <td class="text-end">{{ totalBalance }}</td>
        </tr>
      </tbody>
    </table>
    <nav id="pagination-box-2" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>
  </div>
</template>

<script>
import { searchEntReceipts } from "@/api/receipt.js";
import MyDatePicker from "@/components/my-datepicker.vue";
import MyPagination from "@/components/my-pagination.vue";
import MySelectCustomer from "@/components/my-select2-customer.vue";

export default {
  components: {
    MyDatePicker,
    MyPagination,
    MySelectCustomer,
  },
  data() {
    return {
      dataList: [],
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 10,
        pageTotal: 0,
      },
      beginDate: "",
      endDate: "",
      customerId0: -1,
      status0: 0,
      receiptNo0: "",
      amount0: "",
      totalBalance: 0,
      receiptType: 0,
      likeCustomerName: "",

      modalTitle: "",
      modalNullable: true,
    };
  },
  mounted: function () {
    this.search();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (loadingText) {
      this.$store.commit("showLoading", {
        loading: true,
        loadingText: loadingText,
      });
    },
    hideLoading: function () {
      this.$store.commit("showLoading", {
        loading: false,
      });
    },
    search: function () {
      let amount = Number.parseInt(this.amount0);
      if (isNaN(amount)) {
        amount = 0;
      }
      // console.log(amount)
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        "sc.beginDate": this.beginDate,
        "sc.endDate": this.endDate,
        "sc.customerId": this.customerId0,
        "sc.status": this.status0,
        "sc.receiptNo": this.receiptNo0,
        "sc.amount": amount,
        "sc.receiptType": this.receiptType,
        likeCustomerName: this.likeCustomerName,
      };

      this.showLoading();

      searchEntReceipts(
        params,
        (v) => {
          this.dataList = v.dataList;
          this.sc = v.page;
          this.calcTotalBalance(this.dataList);
        },
        () => {
          this.hideLoading();
        }
      );
    },
    reset: function () {
      this.sc.pageNo = 1;
      this.beginDate = "";
      this.endDate = "";
      this.customerId0 = -1;
      this.status0 = 0;
      this.receiptNo0 = "";
      this.amount0 = "";
      this.receiptType = 0;
      this.likeCustomerName = "";
    },
    commonShowMessage: function (jsonResult) {
      if (jsonResult.status !== "OK") {
        this.showErrMsg("失败：" + jsonResult.errmsg, "danger");
      } else {
        this.search();
        if (jsonResult.desc !== "") {
          this.showErrMsg(jsonResult.desc);
        } else {
          this.showErrMsg("操作成功");
        }
      }
    },
    calcBalance: function (info) {
      return Math.round((info.amount - info.writeOffAmount) * 100) / 100;
    },
    calcTotalBalance: function (infos) {
      this.totalBalance = 0;
      for (const info of this.dataList) {
        this.totalBalance += info.amount - info.writeOffAmount;
      }

      this.totalBalance = Math.round(this.totalBalance * 100) / 100;
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>
